'''

Case Type： 人为因素
Case Name： gsq导入过程中，杀掉后台进程
Create At:  2020/10/12
Owner    :  @peilinqian
Descption: 1、执行gsql导入数据
           2、导入过程中执行kill -9 [pid]
           3、重新执行第1步

history：
created： 2020/10/12  created by @peilinqian
'''
import os
import unittest
from yat.test import Node
import time
from yat.test import macro
import sys
import datetime

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.Constant import Constant
from testcase.utils.Common import Common
from testcase.utils.CommonSH import *
from testcase.utils.ComThread import ComThread

logger = Logger()
common = Common('PrimaryRoot')
commonshpri = CommonSH('PrimaryDbUser')

class ModifyConnection(unittest.TestCase):
    dbPrimaryUserNode = Node(node='PrimaryDbUser')
    dbPrimaryRootNode = Node(node='PrimaryRoot')    
    dbStandby1RootNode = Node(node='Standby1Root')    
    DB_INSTANCE_PATH = macro.DB_INSTANCE_PATH
    DB_ENV_PATH = macro.DB_ENV_PATH
    Constant = Constant()
    dump_file_name = os.path.join(macro.DB_INSTANCE_PATH, 'reliability_humanfactor006.sql')

    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')
    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        logger.info("-----------this is setup-----------")
        logger.info("-----------reliability_humanfactor006 start-----------")
        logger.info("-----------dump success--------------------")        
        result = commonshpri.dumpFile(self.dump_file_name, '-F p')
        logger.info(result)
        flag = 'dump database ' + self.dbPrimaryUserNode.db_name + ' successfully'
        self.assertIn(flag, result)
        self.app_name = self.dump_file_name
        

    def test_modify_pidfile(self):
        logger.info("-----------gsql restore--------------------")        
        gsql_thread = ComThread(commonshpri.gsql_restore, args=(self.dump_file_name,))
        gsql_thread.start()
        
        logger.info("-----------ctl+c--------------------")       
        common.kill_pid_keyword(self.app_name, '9', self.dbPrimaryUserNode)
        
        gsql_thread.join(int(macro.TPCC_RUN_MAX_TIME) * 2)
        logger.info("-----------gsql fail--------------------") 
        result_thread = gsql_thread.get_result()
        logger.info(result_thread)
        flag = 'total time'
        self.assertNotIn(flag, result_thread)
        
        time.sleep(10)
        
        logger.info("-----------gsql success--------------------")        
        result = commonshpri.gsql_restore(self.dump_file_name)
        logger.info(result)
        spend_time_lines = result.strip().splitlines()
        spend_time = spend_time_lines[-1].strip().split()
        logger.info(spend_time)
        if spend_time[2].isnumeric():
            self.assertTrue(int(spend_time[2])>200)
        else:
            logger.info("ctl+c 异常，请检查结果")
            self.assertTrue(False)
        

    def tearDown(self):
        logger.info('----------------this is tearDown-----------------------')
        logger.info('----------------delete dump file-----------------------')
        cmd = 'rm -rf ' + self.dump_file_name
        self.dbPrimaryRootNode.sh(cmd)

        
       